<?php
    class Photo_model extends CI_Model
    {
        var $table = "db_album_cats";
        var $table_photo = "db_album_photos";
        function __construct()
        {
            parent::__construct();
        }
        function list_photo()
        {
            return $this->db->get($this->table_photo)->result();
            $this->db->get($this->table_photo)->free_result();
        }
        function add_photo($data)
        {
            $data = array(
                'name_photo' => $data['name_photo'],
                'name_url'   => $data['name_url'],
                'image'   => $data['image'],                
                'active'   => $data['active'],
                'order_photo'   => $data['order_photo'],
                'id_cats'   => $data['id_cats'],
            );
            $this->db->set($data);
            if($this->db->insert($this->table_photo)) return true; else return false;
        }
        function update_photo($id,$data)
        {
            $this->db->where('id_photo',$id);
            $data = array(
                'name_photo' => $data['name_photo'],
                'name_url'   => $data['name_url'],
                'image'   => $data['image'],
                'active'   => $data['active'],
                'order_photo'   => $data['order_photo'],
                'id_cats'   => $data['id_cats'],
            );
            $this->db->set($data);
            if($this->db->update($this->table_photo)) return true; else return false;
        }
        function get_by_photo($id)
        {
            $this->db->where('id_photo',$id);
            return $this->db->get($this->table_photo)->row();
           
        }
        function change_update_active_photo($id,$data)
        {
            $this->db->where('id_photo',$id);
            $data = array(
                'active' => $data['active']
            );
             $this->db->set($data);
            if($this->db->update($this->table_photo)) return true; else return false;
        }
        function delete_photo($id)
        {
            $this->db->where('id_photo',$id);
            return $this->db->delete($this->table_photo);
        }
        function delete_all_photo_cats($id)
        {
            $q = $this->db->query("DELETE from db_album_photos where id_photo = $id");
          
            if($q)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        function get_by_id_photo($id)
        {
            $q = $this->db->query("select * from db_album_cats,db_album_photos where  db_album_cats.id_album_cats = db_album_photos.id_cats and db_album_photos.id_cats=".$id." order by db_album_photos.id_photo asc");
            return $q->result();
            $q->free_result();
            
        }
         function total($dk = null)
		{
			if($dk != null)
			{
				$this->db->where('id_photo', $dk);
			}
			
			
			
			return $this->db->count_all_results($this->table);
		}
        function photo_phantrang($begin,$max)
		{
			if($begin == ''){
				$begin = 0;
			}
			if($max >= 0 && $max != ''){
				$limit = 'limit '.$begin.','. $max;
			}else{
				$limit ='';	
			}
            
		return $this->db->query('select * from db_album_photos order by id_photo '. $limit)->result();	
		}
        /****************Function Catetgory Album*****************/
        
        function list_cats()
        {
            return $this->db->get($this->table)->result();
            $this->db->get($this->table)->free_result();
        }
        function add_cats($data)
        {
            $data = array(
                'title_cats' => $data['title_cats'],
                'url_title_cats'   => $data['url_title_cats'],
                'meta_keyword'   => $data['meta_keyword'],
                'meta_description'   => $data['meta_description'],
                'active'   => $data['active'],
                'order_cats'   => $data['order_cats'],
            );
            $this->db->set($data);
            if($this->db->insert($this->table)) return true; else return false;
        }
         function update_cats($id,$data)
        {
            $this->db->where('id_album_cats',$id);
            $data = array(
                'title_cats' => $data['title_cats'],
                'url_title_cats'   => $data['url_title_cats'],
                'meta_keyword'   => $data['meta_keyword'],
                'meta_description'   => $data['meta_description'],
                'active'   => $data['active'],
                'order_cats'   => $data['order_cats'],
            );
            $this->db->set($data);
            if($this->db->update($this->table)) return true; else return false;
        }
        function get_by_cats($id)
        {
            $this->db->where('id_album_cats',$id);
            return $this->db->get($this->table)->row();
           
        }
        function change_update_active_cats($id,$data)
        {
            $this->db->where('id_album_cats',$id);
            $data = array(
                'active' => $data['active']
            );
             $this->db->set($data);
            if($this->db->update($this->table)) return true; else return false;
        }
        
        function delete_cats($id)
        {
            $this->db->where('id_album_cats',$id);
            return $this->db->delete($this->table);
        }
        
        
    }
?>